package com.practice.sort;

import java.util.PriorityQueue;

public class NeighborSort {
	public void sort(int[] a, int k) {
		int K = Math.min(k, a.length);
		PriorityQueue<Integer> p = new PriorityQueue<Integer>();
		for (int i=0; i<K; i++) {
			p.add(a[i]);
		}
		
		for (int i=0; i<a.length; i++) {
			a[i] = p.remove();
			if (i+K < a.length) p.add(a[i+K]);
		}
	}
}
